package kr.hyunist.androidcomplete01saeildamil.ch12;

import java.util.*;

import kr.hyunist.androidcomplete01saeildamil.*;
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import android.widget.AdapterView.OnItemSelectedListener;

public class SpinnerTest extends Activity {
	ArrayList<Movie> arList;
	String[] mName;
	Movie mv;
	boolean mInit = false;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.spinnertest);
		
		arList = new ArrayList<Movie>();
		mv = new Movie(R.drawable.poster01, "빅맨", R.drawable.kbs, "(월,화)오후 10:00");
		arList.add(mv);
		mv = new Movie(R.drawable.poster02, "트라이앵글", R.drawable.mbc, "(월,화)오후 10:00");
		arList.add(mv);
		mv = new Movie(R.drawable.poster03, "닥터 이방인", R.drawable.sbs, "(월,화)오후 10:00");
		arList.add(mv);
		mv = new Movie(R.drawable.poster04, "유나의 거리", R.drawable.jtbc, "(월,화)오후 09:50");
		arList.add(mv);
		mv = new Movie(R.drawable.poster05, "골든 크로스", R.drawable.kbs, "(수,목)오후 10:00");
		arList.add(mv);
		mv = new Movie(R.drawable.poster06, "개과천선", R.drawable.mbc, "(수,목)오후 10:00");
		arList.add(mv);

		mName = new String[arList.size()];
		
		for(int i = 0; i < arList.size(); i++){
			mName[i] = arList.get(i).getTitle();
		}
		
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_dropdown_item, mName);
		
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		
		Spinner spin = (Spinner) findViewById(R.id.spinnermoviespinner);
		spin.setAdapter(adapter);
		spin.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				if(mInit == false){
					mInit = true;
					return;
				}
				
				ImageView poster = (ImageView) findViewById(R.id.spinnermovielistposter);
				poster.setBackgroundResource(arList.get(position).getPoster());
				
				TextView title = (TextView) findViewById(R.id.spinnermovielisttitle);
				title.setText(arList.get(position).getTitle());
				
				ImageView broadCast = (ImageView) findViewById(R.id.spinnermovielistbroad);
				broadCast.setBackgroundResource(arList.get(position).getBroadCast());
				
				TextView time = (TextView) findViewById(R.id.spinnermovielisttime);
				time.setText(arList.get(position).getTime());
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				
			}
			
		});
		
	}
}

class Movie {
	private int poster;
	private String title;
	private int broadCast;
	private String time;

	public Movie(int poster, String title, int broadCast, String time) {
		this.poster = poster;
		this.title = title;
		this.broadCast = broadCast;
		this.time = time;
	}

	public int getPoster() {
		return poster;
	}

	public String getTitle() {
		return title;
	}

	public int getBroadCast() {
		return broadCast;
	}

	public String getTime() {
		return time;
	}

	public void setPoster(int poster) {
		this.poster = poster;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public void setBroadCast(int broadCast) {
		this.broadCast = broadCast;
	}

	public void setTime(String time) {
		this.time = time;
	}

}
